// import reactRefresh from "@vitejs/plugin-react-refresh";
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { resolve } from "path";
// import { RollupFilemanager } from "filemanager-plugin/lib/index.js"; // commonjs因此用下面的require引用
// import copy from "rollup-plugin-copy-assets/src/index.js";
const { RollupFilemanager } = require("filemanager-plugin/lib/index.js");

// const copy = require("rollup-plugin-copy-assets/dist/rollup-plugin-copy-assets.js");

import { babel, getBabelOutputPlugin } from "@rollup/plugin-babel";

import typescript from "@rollup/plugin-typescript";

// import legacy from '@vitejs/plugin-legacy'

export default defineConfig({
  css: {
    preprocessorOptions: {
      less: {
        include: ["node_modules"],
      },
    },
  },
  esbuild: {},
  plugins: [
    // reactRefresh(),
    typescript({
      include: ["src/page/nestjs/**/*"],
      exclude: ["node_modules/**/*"],
    }),

    react({
      babel: {
        plugins: [
          // 新版本装饰器不需要自己写babel兼容
          // ["@babel/plugin-proposal-decorators", { legacy: true }], 
          // ["@babel/plugin-proposal-class-properties", { loose: true }]
        ],
      },
    }),
    // copy({
    //   assets: ["public"], // 这样有public文件夹
    // }),
    // legacy({ // 支持ie
    //   targets: ['IE 9', "es5"],
    //   polyfills: true,
    //   renderLegacyChunks: false
    // }),
    // getBabelOutputPlugin({
    //   presets: [
    //     "es2015",
    //     [
    //       "@babel/preset-env",
    //       {
    //         // modules: 'umd', // cjs === exports, umd === c,amd === define,commonjs === exports,
    //         corejs: 2,
    //         useBuiltIns: "entry",
    //         targets: {
    //           // 'browsers': ["ie >= 11"]
    //           ie: "9"
    //         },
    //       },
    //     ],
    //   ],
    // }),
    RollupFilemanager({
      // events: {
      //   start: {
      //     del: {
      //       items: ["./front"],
      //     },
      //   },
      // },
      customHooks: [
        {
          hookName: "buildStart",
          commands: {
            del: {
              items: ["./front"],
            },
          },
        },
        {
          hookName: "writeBundle",
          commands: {
            copy: {
              items: [
                { source: "./public", destination: "./front/public" },
                { source: "./cmaps", destination: "./front" },
              ],
            },
            zip: {
              items: [
                { source: "./front", destination: "./front.zip", type: "zip" },
              ],
            },
          },
        },
      ],
    }),
  ],
  publicDir: "public111", // 复制静态文件到出口（取个没有的文件夹因为这个配置只会将对应文件夹的文件复制到目标文件加下并且不会保留父文件夹（这里我是为了保存文件夹做了这个设置））
  server: {
    port: 3322,
    hmr: true,
    proxy: {
      "/api": {
        target: "http://localhost:1122",
        // target: "http://115.28.106.143:1122",
        changeOrigin: true,
        // rewrite: (path) => path.replace(/^\/api/, '')
      },
      "/testapi": {
        target: "http://localhost:8080",
        // target: "http://115.28.106.143:1122",
        changeOrigin: true,
        // rewrite: (path) => path.replace(/^\/api/, '')
      },
    },
  },
  resolve: {
    alias: {
      "@": resolve("./src"),
    },
  },
  base: "/front",
  build: {
    // lib: {
    //   formats: ["cjs", "umd"],
    //   name: 'test',
    //   entry: "./src/main.tsx"
    // },
    outDir: "front",
    rollupOptions: {
      treeshake: false,
      output: {
        plugins: [],
      },
    },
    target: "es6", // 最低只能支持到es6
  },
});
